<?php
class page extends spController
{
	function index(){ // 页面管理
		$this->tpl_title = "页面管理";  
		$this->results = spClass("lib_contents")->spPager($this->spArgs('page', 1), 10)->findAll(array("type" => 'page'),"cid DESC"); 
		$this->pager = spClass("lib_contents")->spPager()->getPager();
		$this->display("admin/page_index.html");
	}
	
	function write(){ // 撰写页面
		$this->tpl_title = "新建页面";
		$this->formUrl = spUrl("page", "add");
		$dir = opendir(DIY_TPL_PATH);
    	while ($fileDir = readdir($dir)) {
			if (!strcmp($fileDir,".")||!strcmp($fileDir,"..")) {
				continue;
			}
			if (substr($fileDir, 0,4)=="diy_"){
				$arr[]=$fileDir;
				$this->options = $arr;
			}
		}
		closedir($dir);
		$this->display("admin/page_write.html");
	}
	
	function add(){ // 添加页面
		$user = spClass("lib_contents");
		$newrow = array( // 这里制作新增记录的值
			'url' => $this->spArgs('url'),
			'title' => $this->spArgs('title'),
			'template' => $this->spArgs('template'),
			'created' => mktime(),
			'text' => $this->spArgs('text'),
			'keywords' => $this->spArgs('keywords'),
			'overview' => $this->spArgs('overview'),
			'uid' => $_SESSION["userinfo"]["uid"],
			'type' => 'page',
		);
		$user->create($newrow); 
		$this->success("页面撰写成功！", spUrl("page", "index"));
	}

	function edit(){ // 页面修改
		$this->tpl_title = "编辑页面";
		$this->formUrl = spUrl("page", "update");
		$dir = opendir(DIY_TPL_PATH);
    	while ($fileDir = readdir($dir)) {
			if (!strcmp($fileDir,".")||!strcmp($fileDir,"..")) {
				continue;
			}
			if (substr($fileDir, 0,4)=="diy_"){
				$arr[]=$fileDir;
				$this->options = $arr;
			}
		}
		closedir($dir);
		// 这里先判断是否传入了id
		if( $cid = $this->spArgs("cid") ){
			// 查找表，获取信息
			$this->info = spClass("lib_contents")->find(array('cid'=>$cid));
			$this->display("admin/page_edit.html");
		}else{
			// 无id则直接跳转回首页
			$this->jump(spUrl("page","index"));
		}
	}	

	function update(){ //更新
		$post = spClass("lib_contents");
		// 这里先判断是否传入了id
		if( $cid = $this->spArgs("cid") ){
			// 执行删除
		$conditions = array("cid"=>$cid); 
		$newrow = array(
			'url' => $this->spArgs('url'),
			'title' => $this->spArgs('title'),
			'template' => $this->spArgs('template'),
			'modified' => mktime(),
			'text' => $this->spArgs('text'),
			'keywords' => $this->spArgs('keywords'),
			'overview' => $this->spArgs('overview'),
			'uid' => $_SESSION["userinfo"]["uid"],
			);
		$post->update($conditions, $newrow); // 更新记录
		$this->success("更新页面成功！", spUrl("page", "index"));
		}
	}

	function del(){// 删除页面
		// 这里先判断是否传入了id
		if( $cid = $this->spArgs("cid") ){
			// 执行删除
			spClass("lib_contents")->delete(array('cid'=>$cid));
			$this->success("删除成功！", spUrl("page","index"));
		}else{
			// 无id则直接跳转回首页
			$this->jump(spUrl("page","index"));
		}
	}	
	
}